Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return signing key in SignedEnvelope.payload #2522

Merged
merged 4 commits into from
Feb 21, 2022

Conversation

MarcoPolo
Copy link
Contributor

Context

Fixes #2511.

Another solution would be to pass in a closure as a key validation function to payload. I originally tried this, but quickly noticed that it made the PeerRecord use case awkward. This is because the peer id we are validating against is inside the payload. So the closure would need access to the payload. If you passed in the payload to the closure, then you would need to parse the payload twice (or awkwardly mutate an outer variable).

So instead, I return the signing key from the payload method.

If I'm misunderstanding something @thomaseizinger please let me know.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! 🙏

Comment on lines +52 to +54
/// It is the caller's responsibility to check that the signing key is what
/// is expected. For example, checking that the signing key is from a
/// certain peer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about enforcing this constraint via #[must_use]? Something along the lines of the playground below:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1a32dedc2d59c3298dc73a8b48d9d76c

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh excellent! I was wondering if there was a way I could leverage #[must_use]. Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, unfortunately one can not do it on an anonymous tuple. E.g. instead of the additional struct I would prefer to do:

fn foo() -> Result<(a, #[must_use] b), c> {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that was my first thought, but I hadn't considering using a struct instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, the above suggestion actually does not work. The compiler reports the field not being used in general, rather than not being used in this particular case.

Also nesting #[must_use] structs doesn't work see rust-lang/rust#39524.

Also @MarcoPolo pointed out that users would likely do something along the lines of let (payload, _) = payload().handle_error(); which would thus circumvent the #[must_use].

Sorry for the noise here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do something like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=92e6fc6cf971873eb590d9ada72f85fa

But I am not sure if it is useful. Assigning _ will silence the must_use warning ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately assigning the return value to anything and not using the key value will also satisfy the must_use lint. e.g. let data = foo().unwrap();

///
/// It is the caller's responsibility to check that the signing key is what
/// is expected. For example, checking that the signing key is from a
/// certain peer.
pub fn payload(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn payload(
pub fn payload_and_signing_key(

What do you think of renaming the method as well? If I recall correctly you suggested this in your initial vulnerability report, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in ef3afcd

Copy link
Contributor

@thomaseizinger thomaseizinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling this!

@mxinden
Copy link
Member

mxinden commented Feb 18, 2022

I suggest we move forward and merge here. @MarcoPolo given that this is a breaking change, would you mind adding a changelog entry to core/CHANGELOG.md?

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

@mxinden mxinden merged commit 6511e6b into libp2p:master Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

core/src/signed_envelope: Enforce verification of public key on SignedEnvelope::payload
3 participants